package com.xxx.client.android.dialog;

import android.app.Activity;
import android.content.Intent;

import com.xxx.client.android.activity.ArticleListActivity;
import com.xxx.client.android.api.ArticleListEntry;
import com.xxx.client.android.api.WSError;
import com.xxx.client.android.api.XXXApi;
import com.xxx.client.android.api.impl.XXXApiImpl;

public class ArticleListLoadingDialog extends
		LoadingDialog<String, ArticleListEntry> {

	public ArticleListLoadingDialog(Activity activity, int loadingMsg,
			int failMsg) {
		super(activity, loadingMsg, failMsg);
	}

	/**
	 * @param params
	 *            params[0]:keyword params[1]:type params[2]:submenuId params[3]:submenuName
	 * */
	@Override
	public ArticleListEntry doInBackground(String... params) {

		XXXApi service = new XXXApiImpl();
		ArticleListEntry articleList = null;
		XXXApi.GetArticleNameListParam getArticleNameListParam = new XXXApi.GetArticleNameListParam();
		getArticleNameListParam.setKeyword(params[0]);
		getArticleNameListParam.setType(params[1]);
		getArticleNameListParam.setSubmenuId(Integer.parseInt(params[2]));
		try {
			articleList = service.getArticleNameListByObject(getArticleNameListParam);
			articleList.setHeader(params[3]);
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		} catch (WSError e) {
			publishProgress(e);
			cancel(true);
		}
		return articleList;

	}

	@Override
	public void doStuffWithResult(ArticleListEntry articleList) {

		Intent intent = new Intent(mActivity, ArticleListActivity.class);

		intent.putExtra("articleList", articleList);
		mActivity.startActivity(intent);
		if (mActivity instanceof ArticleListActivity) {
			mActivity.finish();
		}
	}

}
